/*
 * Settings.cpp
 *
 *  Created on: 09.05.2011
 *      Author: mifodix
 */

#include "Settings.hpp"

Settings::Settings()
{
	subnet=0;
	netmask=0;
	server=0;
	rangeBegin=0;
	rangeEnd=0;
	defaultLeaseTime=0;
	maxLeaseTime=0;
}

Settings::~Settings()
{
}

uint32_t Settings::GetDefaultLeaseTime() const
{
    return defaultLeaseTime;
}

std::list<uint32_t> Settings::GetDomainNameServers() const
{
    return domainNameServers;
}

uint32_t Settings::GetMaxLeaseTime() const
{
    return maxLeaseTime;
}

uint32_t Settings::GetNetmask() const
{
    return netmask;
}

uint32_t Settings::GetRangeBegin() const
{
    return rangeBegin;
}

uint32_t Settings::GetRangeEnd() const
{
    return rangeEnd;
}

std::list<uint32_t> Settings::GetRouters() const
{
    return routers;
}

uint32_t Settings::GetServer() const
{
    return server;
}

uint32_t Settings::GetSubnet() const
{
    return subnet;
}

void Settings::SetDefaultLeaseTime(uint32_t defaultLeaseTime)
{
    this->defaultLeaseTime = defaultLeaseTime;
}

void Settings::SetMaxLeaseTime(uint32_t maxLeaseTime)
{
    this->maxLeaseTime = maxLeaseTime;
}

void Settings::SetNetmask(uint32_t netmask)
{
    this->netmask = netmask;
}

void Settings::SetRangeBegin(uint32_t rangeBegin)
{
    this->rangeBegin = rangeBegin;
}

void Settings::SetRangeEnd(uint32_t rangeEnd)
{
    this->rangeEnd = rangeEnd;
}

void Settings::SetServer(uint32_t server)
{
    this->server = server;
}

void Settings::SetSubnet(uint32_t subnet)
{
    this->subnet = subnet;
}

bool Settings::AddRouter(const char *router)
{
	uint32_t ip=NetUtils::String2IP(router);
	if (ip>0)
	{
		return AddRouter(ip);
	}
	return false;
}

bool Settings::AddDomainNameServer(const char *dns)
{
	uint32_t ip=NetUtils::String2IP(dns);
	if (ip>0)
	{
		return AddDomainNameServer(ip);
	}
	return false;
}

bool Settings::AddDomainNameServer(uint32_t dns)
{
	if (std::find(domainNameServers.begin(), domainNameServers.end(), dns)!=domainNameServers.end())
	{
		return false;
	}
	domainNameServers.push_back(dns);
	return true;
}

uint32_t Settings::FindReservedIP(const char *mac)
{
	for (std::list<MacIP>::iterator it=reservedIPs.begin(); it!=reservedIPs.end(); ++it)
	{
		if (*it ==mac) return it->GetIP();
	}
	return 0;
}

bool Settings::FindReservedIP(uint32_t ip)
{
	for (std::list<MacIP>::iterator it=reservedIPs.begin(); it!=reservedIPs.end(); ++it)
	{
		if (*it ==ip) return true;
	}
	return false;
}

bool Settings::AddReservedIP(const char *mac, size_t macSize, const char *ip)
{
	uint32_t convertedIP=NetUtils::String2IP(ip);
	if (convertedIP>0)
	{
		return AddReservedIP(mac, macSize, convertedIP);
	}
	return false;
}

bool Settings::SetNetmask(const char *netmask)
{
	uint32_t ip=NetUtils::String2IP(netmask);
	if (ip>0)
	{
		SetNetmask(ip);
		return true;
	}
	return false;
}

bool Settings::SetServer(const char *server)
{
	uint32_t ip=NetUtils::String2IP(server);
	if (ip>0)
	{
		SetServer(ip);
		return true;
	}
	return false;
}

bool Settings::SetSubnet(const char *subnet)
{
	uint32_t ip=NetUtils::String2IP(subnet);
	if (ip>0)
	{
		SetSubnet(ip);
		return true;
	}
	return false;
}

bool Settings::AddRouter(uint32_t router)
{
	if (std::find(routers.begin(), routers.end(), router)!=routers.end())
	{
		return false;
	}
	routers.push_back(router);
	return true;
}

bool Settings::AddReservedIP(const char *mac, size_t macSize, uint32_t ip)
{
	for (std::list<MacIP>::iterator it=reservedIPs.begin(); it!=reservedIPs.end(); ++it)
	{
		if (*it ==mac) return false;
	}
	MacIP rIP(mac, macSize, ip);
	reservedIPs.push_back(rIP);
	return true;
}

bool Settings::SetRangeEnd(const char *rangeEnd)
{
	uint32_t ip=NetUtils::String2IP(rangeEnd);
	if (ip>0)
	{
		SetRangeEnd(ip);
		return true;
	}
	return false;
}

bool Settings::SetRangeBegin(const char *rangeBegin)
{
	uint32_t ip=NetUtils::String2IP(rangeBegin);
	if (ip>0)
	{
		SetRangeBegin(ip);
		return true;
	}
	return false;
}

bool Settings::IsInRange(uint32_t ip)
{
	if (rangeBegin==0 || rangeEnd==0 || ip<rangeBegin || ip>rangeEnd)
	{
		return false;
	}
	return true;
}
